package com.lexsoft.kanaiscube.calc;

import java.text.DecimalFormat;

import com.lexsoft.kanaiscube.R;
import com.lexsoft.kanaiscube.util.Tools;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class CdrCalculator extends Fragment implements OnClickListener,OnFocusChangeListener{
	View view;		
	ListView list;
	SimpleAdapter sa;
	ImageView[] heroes;
	int[] images,gimages;
	int[][] cdrMaxValues;
	private static int _PARAMS = 12;
	private int current_hero=0;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_cdr_calculator, container,
				false);
		ScrollView sv = (ScrollView)view.findViewById(R.id.scrollCdr);
		sv.measure(ScrollView.MEASURED_SIZE_MASK, ScrollView.MEASURED_HEIGHT_STATE_SHIFT);
		sv.setFillViewport(true);
		
		
		Log.d("lex",""+sv.isFillViewport());
		
		String[] heroList = getResources().getStringArray(R.array.cdr_class_name);
		String[] imageList = getResources().getStringArray(R.array.cdr_image_name);
		images = new int[imageList.length];
		gimages = new int[imageList.length];
		for(int i=0;i<imageList.length;i++){
			images[i] = Tools.getResourceIdByName("drawable", imageList[i]);
			gimages[i] = Tools.getResourceIdByName("drawable", "g"+imageList[i]);
		}
		heroes = new ImageView[heroList.length];
		for(int i=0;i<heroList.length;i++){
			View v = view.findViewById(Tools.getResourceIdByName("id",heroList[i]));
			heroes[i]=(ImageView)v;
			v.setOnClickListener(this);
		}
		String[] mvs = getResources().getStringArray(R.array.cdr_max_values);
		cdrMaxValues = new int[mvs.length][_PARAMS];
		for(int i=0;i<mvs.length;i++){
			String[] infos = mvs[i].split(",");
			for(int j=0;j<_PARAMS&&j<infos.length;j++){
				cdrMaxValues[i][j] = Integer.parseInt(infos[j]);
			}
		}
		for(int i=0;i<_PARAMS;i++){
			TextView v = (TextView)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+(i+1)));
			v.setOnFocusChangeListener(this);
		}
		setMaxTips();
		
		view.findViewById(R.id.cdrButtonCalc).setOnClickListener(this);
		view.findViewById(R.id.cdrButtonClear).setOnClickListener(this);
		view.findViewById(R.id.cdrButtonMax).setOnClickListener(this);
		calcCdrResult();
		return view;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	private static final DecimalFormat sdf = new DecimalFormat("0.#%");
	private static final DecimalFormat ddf = new DecimalFormat("0.00%");
	private static final DecimalFormat isdf = new DecimalFormat("0.#");
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(v instanceof ImageView){
			for(int i=0;i<heroes.length;i++){
				if(id==heroes[i].getId()){
					heroes[i].setImageDrawable(getResources().getDrawable(images[i]));
					current_hero = i;
				}else{
					heroes[i].setImageDrawable(getResources().getDrawable(gimages[i]));
				}
			}
			setMaxTips();
		}else{
			switch(id){
			case R.id.cdrButtonCalc:
				calcCdrResult();
				break;
			case R.id.cdrButtonClear:
				clearValues();
				break;
			case R.id.cdrButtonMax:
				setMaxValues();
				break;
			default:
					break;
			}
		}
	}

	private void setMaxTips(){
		String cap = getResources().getString(R.string.max_text);
		for(int i=0;i<_PARAMS;i++){
			TextView v = (TextView)view.findViewById(Tools.getResourceIdByName("id", "cdrTipsText_"+(i+1)));
			v.setText("("+cap+" "+sdf.format(cdrMaxValues[current_hero][i]/10000.0)+")");
		}
	}
	
	private void setMaxValues(){
		for(int i=0;i<_PARAMS;i++){
			EditText v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+(i+1)));
			v.setText(isdf.format(cdrMaxValues[current_hero][i]/100.0));
		}
	}
	private void clearValues(){
		for(int i=0;i<=_PARAMS;i++){
			EditText v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+(i+1)));
			v.setText("");
		}
		((TextView)view.findViewById(R.id.cdrCalcResult)).setText(getString(R.string.cdr_calc_result));
	}
	private void calcCdrResult(){
		double result = 1.0;
		EditText v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_1"));
		String str = (v.getText()==null?"":v.getText().toString());
		double diamond = -1;
		if(!"".equals(str)){
			diamond = Double.parseDouble(str)/100;
		}
		if(diamond>0){
			v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+_PARAMS));
			str = (v.getText()==null?"":v.getText().toString());
			if(!"".equals(str)){
				diamond *= (1+Double.parseDouble(str)/100);
			}
			result *= (1-diamond);
		}
		for(int i=2;i<_PARAMS;i++){
			v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+i));
			str = (v.getText()==null?"":v.getText().toString());
			if(!"".equals(str)){
				double single = Double.parseDouble(str)/100;
				result *= (1- single);
			}
		}
		v = (EditText)view.findViewById(Tools.getResourceIdByName("id", "cdrInputText_"+(_PARAMS+1)));
		str = (v.getText()==null?"":v.getText().toString());
		if(!"".equals(str)){
			String[] extras = str.split(",");
			double extra = 0;
			for(int i=0;i<extras.length;i++){
				try{
					extra = Double.parseDouble(extras[i])/100;
				}catch(Exception e){
					extra = 0;
				}
				result *= (1-extra);
			}
		}
		TextView tv = (TextView)view.findViewById(R.id.cdrCalcResult);
		String cap = getString(R.string.cdr_calc_result);
		tv.setText(cap + " "+ ddf.format(1-result));
	}

	@Override
	public void onFocusChange(View view, boolean hasFocus) {
		// TODO Auto-generated method stub
		
	}

}
